jetcrab\bytecode\expressions/
arithmetic.rs1use crate::ast::Node;
2use crate::vm::instructions::Instruction;
3
4pub trait ArithmeticGenerator {
5 fn generate_binary_expression(&mut self, node: &Node);
6}
7
8pub trait ArithmeticCore {
9 fn instructions(&mut self) -> &mut Vec<Instruction>;
10 fn visit_node(&mut self, node: &Node);
11}
12
13impl<T> ArithmeticGenerator for T
14where
15 T: ArithmeticCore,
16{
17 fn generate_binary_expression(&mut self, node: &Node) {
18 if let Node::BinaryExpression(expr) = node {
19 self.visit_node(&expr.left);
20 self.visit_node(&expr.right);
21 match expr.operator.as_str() {
22 "+" => self.instructions().push(Instruction::Add),
23 "-" => self.instructions().push(Instruction::Sub),
24 "*" => self.instructions().push(Instruction::Mul),
25 "/" => self.instructions().push(Instruction::Div),
26 "%" => self.instructions().push(Instruction::Mod),
27 "**" => self.instructions().push(Instruction::Exp),
28 _ => {
29 self.instructions().push(Instruction::Add);
30 }
31 }
32 }
33 }
34}